﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectScheduling
{
    public partial class CheckTimesForm : Form
    {
        public static readonly CheckTimesForm b = new();
        private readonly List<(DateTime start, DateTime end)> times = [];
        private CheckTimesForm()
        {
            InitializeComponent();
        }
        public void Init(List<(DateTime start, DateTime end)> time)
        {
            if (time.Count == 0)
            {
                time = [(DateTime.Today, DateTime.Today.AddHours(18))];
            }
            p_main.Controls.Clear();
            times.Clear();
            foreach (var t in time)
            {
                add(t.start, t.end);
            }
        }
        private void add(DateTime start, DateTime end)
        {
            var obj = (start, end);
            times.Add(obj);
            var p = new Panel
            {
                Dock = DockStyle.Top,
                Height = 40,
                BorderStyle = BorderStyle.FixedSingle,
            };
            var pl = new Panel
            {
                Dock = DockStyle.Right,
                Width = 40
            };
            var p2 = new Panel
            {
                Dock = DockStyle.Fill,
                Width = 400
            };
            var width = 200;
            var time1 = new DateTimePicker
            {
                Width = width - 20,
                Value = start,
                Location = new Point(7, 7),
                MinDate = DateTime.Today,
                Format = DateTimePickerFormat.Custom,
                CustomFormat = "yyyy-MM-dd日HH时"
            };
            var time2 = new DateTimePicker
            {
                Width = width - 20,
                Value = end,
                Location = new Point(width, 7),
                MinDate = start,
                Format = DateTimePickerFormat.Custom,
                CustomFormat = "yyyy-MM-dd日HH时"
            };
            time1.ValueChanged += (o, e) =>
            {
                time2.MinDate = obj.start = time1.Value;
            };
            time2.ValueChanged += (o, e) =>
            {
                obj.end = time2.Value;
            };
            p2.Controls.Add(time1);
            p2.Controls.Add(time2);
            var label = new Label
            {
                AutoSize = false,
                Text = "×",
                Font = new Font("宋体", 20),
                Width = 40,
                Height = 40,
                TextAlign = ContentAlignment.MiddleCenter,
                Cursor = Cursors.Hand
            };
            label.Click += (o, e) =>
            {
                if (times.Count > 1)
                {
                    var idx = times.FindIndex(it => it == obj);
                    if (idx < 0) return;
                    times.RemoveAt(idx);
                    p_main.Controls.RemoveAt(idx);
                }
            };
            pl.Controls.Add(label);
            p.Controls.Add(pl);
            p.Controls.Add(p2);
            p_main.Controls.Add(p);
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            var max = times.Max(it => it.end);
            var next = max.AddHours(24 - max.Hour);
            add(next, next.AddHours(18));
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            #region 检验是否有冲突的时间
            //if (times.Any(obj =>times.Where(z => z != obj).Any(z=>obj.start>=z.start && obj.start <= z.end))){
            //    MessageBox.Show("时间段有冲突", "错误提示");
            //    return;
            //}
            foreach (var t in times)
            {
                var other = times.Where(o => t != o).ToList();
                foreach(var t1 in other)
                {
                    if(t.start>=t1.start && t.end <= t1.end)
                    {
                        MessageBox.Show("时间段有冲突","错误提示");
                        return;
                    }
                }
            }
            #endregion
        }
    }
}
